/*****************************************************************************
Last Edited: 	8/21/2019 NJ
Description:

This program runs MDRD2 and MDRD1 pscores at the program-level

*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

args bw

* Settings
set trace off
set tracedepth 1
set more off

forval year = 2016/2018{

	* Save MDRD1 pscores as tempfile to merge in
	use "${cleandata}program_pscore_nobw_ms_`year'.dta", clear
	keep stu programcode pscore_form pscore_freq
	ren pscore_* pscore_*_no_bw
	tempfile nobw
	sa "`nobw'"

	* Start with MDRD2 (general risk) pscores
	use "${cleandata}program_pscore_`bw'`modification_str'_ms_`year'.dta", clear

	* We are going to use the School DBN (District, Borough, Number) as
	* unique identifier for the school variable (sch)
	preserve
		keep finaloffer finalofferdbn
		duplicates drop
		ren finaloffer programcode
		ren finalofferdbn sch_long
		tempfile dbns
		sa "`dbns'"
	restore

	gen programcode_suffix = programcode
	replace programcode = stripped_programcode
	merge m:1 programcode using "`dbns'", keep(1 3)  gen(dbn_merge)
	replace programcode = programcode_suffix

	* Merge in MDRD1 pscores
	merge 1:1 stu programcode using "`nobw'", nogen

	* Checks for if we assign same program
	gen same_prg = mrmatch == stripped_match

	* Check missing lotto (is 0 by construction due to lotto simulation)
	gen missing_lotto = lottery_rank == .

	* Screened use lottery, but not edopt
	*gen screened_with_lottery_noedopt = screened_uses_lottery == 1 & description != "Ed. opt."
	gen screened_with_lottery = screened_uses_lottery

	* Generate indicator for number of offers by program
	bys stu programcode : egen offer_prg = max(offer)
	la var offer_prg "Indicator variable, = 1 if student got offer from program, 0 otherwise"

	// Merge in our simulated offers
	merge m:1 stu using "${cleandata}best_guess_simulated_match_ms_`year'_nonsped.dta", nogen keep(1 3) keepusing(ourmatch)
	ren ourmatch ourmatch2

	// Generate offer variable for our match
	gen sim_offer_prg = programcode == stripped_match

	bys programcode: egen screened_with_lottery_temp = max(screened_with_lottery)
	replace screened_with_lottery = screened_with_lottery_temp
	drop screened_with_lottery_temp

	* Only need to aggregate by program for DIA policy programs (started from 2017)
	if `year' >= 2017{
		bys stu programcode: egen choice_temp = min(choice)
		replace choice = choice_temp
		drop choice_temp
	}

	// Aggegrate pscores for DIA programs by program
	foreach score in pscore_form pscore_form_bw pscore_form_no_bw ///
		pscore_freq pscore_freq_no_bw pscore_qbw  {
		bys stu programcode : egen double `score'_prg = total( `score' )
	}

	keep stu programcode sch_long desc offer_prg sim_offer_prg finalofferdbn pscore_form_prg ///
			pscore_*_prg choice missing_lotto same_prg screened_with_lottery

	duplicates drop
	isid stu programcode

	sa "${cleandata}/prg_level_pscores_`bw'_ms_`year'.dta", replace

}
